﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JiuMeiMall.PMethod
{
    /// <summary>
    /// 日志写入
    /// </summary>
    public class LogWriter
    {
        public static object logLock = new object();
        /// <summary>
        /// 写日志文件
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="Ptxt"></param>
        public static void Write(string Type, string Ptxt)
        {
            lock (logLock)
            {
                string path = System.AppDomain.CurrentDomain.BaseDirectory;
                path += "\\Log";
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                path += "\\" + Type + "(" + DateTime.Now.ToString("yyyyMMdd") + ").log";
                System.IO.File.AppendAllText(path, Ptxt + "\r\n");
            }
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="ex"></param>
        public static void WriteEx(string Type, System.Exception ex)
        {
            lock (logLock)
            {
                string path = System.AppDomain.CurrentDomain.BaseDirectory;
                path += "\\LogEx";
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                path += "\\" + Type + "(" + DateTime.Now.ToString("yyyyMMdd") + ").log";
                string Ptxt = DateTime.Now + ": " + ex.Message + "\r\n" + ex.Source + "\r\n" + ex.StackTrace;
                System.IO.File.AppendAllText(path, Ptxt + "\r\n");

            }
        }
    }
}
